package xc.controller;


import xc.dao.RoomDao;
import xc.entity.Room;
import xc.service.RoomService;
import xc.utils.Menu;
import xc.utils.GetNumber;

import java.sql.SQLException;
import java.util.Scanner;

public class RoomController {

    private RoomService roomService;
    private RoomDao roomDao;
    private Scanner scanner;
    private GetNumber getNumber;

    public  RoomController() {
        this.getNumber = new GetNumber();
        this.roomService = new RoomService();
        this.roomDao = new RoomDao();
        this.scanner = new Scanner(System.in);
    }


    public void functionRoom() throws SQLException {
        while(true){
            Menu.chooseRoom();

            int option = GetNumber.ToInt();
            switch (option){
                case 1://增加房间
                    addRoom();
                    break;
                case 2://删除房间
                    deleteRoom();
                    break;
                case 3://修改房间名和状态
                    updateRoom();
                    break;
                case 4://查询房间
                    selectRoom();
                    break;
                case 5://退出
                    System.out.println("成功退出");
                    return;
            }
        }
    }

    private void selectRoom() throws SQLException {
        roomService.selectRoom();
    }

    private void updateRoom() throws SQLException {
        System.out.println("请输入要修改的房间名");
        String roomName = scanner.nextLine();
        System.out.println("请输入该房间需要修改的状态");
        String roomStatus = scanner.nextLine();
        roomService.updateRoom(roomName,roomStatus);
    }

    private void deleteRoom() throws SQLException {
        System.out.println("请输入要删除的房间名");
        String roomName = scanner.nextLine();
        if (roomService.deleteRoom(roomName)) {
            System.out.println("删除失败");
        }else  {
            System.out.println("删除成功");
        }
    }

    private void addRoom() throws SQLException {
        System.out.println("请输入要添加的房间名:");
        String roomName = scanner.nextLine();
        System.out.println("房间的类型是:");
        String roomType = scanner.nextLine();
        Room room = new Room(roomName, roomType);
        if (roomService.addRoom(room))
            System.out.println("添加成功");
        else
            System.out.println("添加失败");
    }
}
